﻿using System;

using System.Collections;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Global.Class.Library;
using BusinessLogicLayer;
using DataAccessLayer;
using System.Globalization;

using DataTransferObject;
using BrightIdeasSoftware;


namespace PresentationLayer
{
    public partial class MarketingProgram : Form, IMergeToolStrip, ICallToolStrip
    {

        #region <Implement Interface>

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        public GlobalEnum.TaskID TaskID
        {
            get { return this.marketingProgramBLL.TaskID; }
        }


        public ToolStrip ChildToolStrip
        {
            get
            {
                return this.toolStripChildForm;
            }
            set
            {
                this.toolStripChildForm = value;
            }
        }





        #region Context toolbar


        public bool IsDirty
        {
            get { return this.marketingProgramBLL.IsDirty; }
        }

        public bool IsValid
        {
            get { return this.marketingProgramBLL.IsValid; }
        }

        public bool Closable
        {
            get { return true; }
        }

        public bool Loadable
        {
            get { return true; }
        }

        public bool Newable
        {
            get { return !this.marketingProgramBLL.ReadOnly; }
        }

        public bool Editable
        {
            get { return !this.marketingProgramBLL.ReadOnly; }//get { return this.marketingProgramBLL.Editable; }
        }

        public bool Deletable
        {
            get { return this.marketingProgramBLL.Editable; }
        }

        public bool Importable
        {
            get { return true; }
        }

        public bool Exportable
        {
            get { return true; }
        }

        public bool Verifiable
        {
            get { return this.marketingProgramBLL.Verifiable; }
        }

        public bool Unverifiable
        {
            get { return this.marketingProgramBLL.Unverifiable; }
        }

        public bool Printable
        {
            get { return false; }
        }

        public bool Searchable
        {
            get { return true; }
        }

        /// <summary>
        /// Edit Mode: True, Read Mode: False
        /// </summary>
        private bool editableMode;
        private int lastMarketingProgramID;
        public bool EditableMode { get { return this.editableMode; } }
        /// <summary>
        /// This reverse of the EditableMode
        /// </summary>
        public bool ReadonlyMode { get { return !this.editableMode; } }

        /// <summary>
        /// Set the editableMode
        /// </summary>
        /// <param name="editableMode"></param>
        private void SetEditableMode(bool editableMode)
        {
            if (this.editableMode != editableMode)
            {
                this.lastMarketingProgramID = this.marketingProgramBLL.MarketingProgramID;
                this.editableMode = editableMode;
                this.NotifyPropertyChanged("EditableMode");

                this.toolStripMenuCustomerImport.Enabled = this.editableMode;
            }
        }


        private void CancelDirty(bool restoreSavedData)
        {
            try
            {
                if (restoreSavedData || this.marketingProgramBLL.MarketingProgramID <= 0)
                    this.marketingProgramBLL.Fill(this.lastMarketingProgramID);

                this.SetEditableMode(false);
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }



        #endregion Context toolbar



        #region ICallToolStrip

        public void Escape()
        {
            if (this.EditableMode)
            {
                if (this.IsDirty)
                {
                    DialogResult dialogResult = MessageBox.Show(this, "Do you want to save your change?", "Warning", MessageBoxButtons.YesNoCancel);
                    if (dialogResult == DialogResult.Yes)
                    {
                        this.Save();
                        if (!this.IsDirty) this.CancelDirty(false);
                    }
                    else if (dialogResult == DialogResult.No)
                    {
                        this.CancelDirty(true);
                    }
                    else
                        return;
                }
                else
                    this.CancelDirty(false);
            }
            else
                this.Close(); //Unload this module
        }

        //Can phai xem lai trong VB de xem lai this.SetEditableMode () khi can thiet

        public void Loading()
        {
            this.GetMasterList();
        }

        public void New()
        {
            this.marketingProgramBLL.New();
            this.SetEditableMode(true);
        }

        public void Edit()
        {
            this.SetEditableMode(true);
        }

        public void Save()
        {
            this.marketingProgramBLL.Save();
            this.GetMasterList();
        }

        public void Delete()
        {
            DialogResult dialogResult = MessageBox.Show(this, "Are you sure you want to delete " + this.marketingProgramBLL.MarketingProgramMaster.Reference + "?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Stop);
            if (dialogResult == DialogResult.Yes)
            {
                try
                {
                    this.marketingProgramBLL.Delete();
                    this.GetMasterList();
                }
                catch (Exception exception)
                {
                    GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
                }
            }
        }

        public void Import()
        {
            this.ImportExcel(OleDbDatabase.MappingTaskID.MarketingProgram);
        }

        public void Export()
        {
            try
            {
                if (this.ActiveControl.Equals(this.dataListViewMaster))
                {
                    DataTable dataTableExport;
                    dataTableExport = this.dataListViewMaster.DataSource as DataTable;
                    if (dataTableExport != null) CommonFormAction.Export(dataTableExport);
                }
                else
                {
                    List<MarketingProgramCustomerName> listExport;
                    listExport = this.marketingProgramBLL.CustomerNameList.ToList();
                    if (listExport != null) CommonFormAction.Export(listExport);
                }
            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }
        }

        public void Verify()
        {
            MessageBox.Show("Verify");
        }

        public void Print(PrintDestination printDestination)
        {
            MessageBox.Show("Print");
        }

        public void SearchText(string searchText)
        {
            CommonFormAction.OLVFilter(this.dataListViewMaster, searchText);
        }

        #endregion


        #endregion <Implement Interface>








        #region <Storage>
        private MarketingProgramBLL marketingProgramBLL;

        #endregion <Storage>


        public MarketingProgram()
        {
            try
            {
                InitializeComponent();

                InitializeTabControl();

                this.marketingProgramBLL = new MarketingProgramBLL();

                this.marketingProgramBLL.PropertyChanged += new PropertyChangedEventHandler(marketingProgramBLL_PropertyChanged);

            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }
        }

        void marketingProgramBLL_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            this.NotifyPropertyChanged(e.PropertyName);
        }


        private void InitializeTabControl()
        {
            try
            {

                CustomTabControl customTabControlCustomerChannel = new CustomTabControl();
                CustomTabControl customTabControlMechanicScheme = new CustomTabControl();
                CustomTabControl customTabControlItemCategory = new CustomTabControl();

                customTabControlCustomerChannel.ImageList = this.imageListTabControl;
                customTabControlMechanicScheme.ImageList = this.imageListTabControl;
                customTabControlItemCategory.ImageList = this.imageListTabControl;


                customTabControlCustomerChannel.TabPages.Add("CustomerChannel", "Channel    ", 6);
                customTabControlCustomerChannel.TabPages[0].Controls.Add(this.dataGridViewCustomerChannel);
                this.dataGridViewCustomerChannel.Dock = DockStyle.Fill;

                customTabControlCustomerChannel.TabPages.Add("CuatomerName", "Customer", 0);
                customTabControlCustomerChannel.TabPages[1].Controls.Add(this.dataGridViewCustomerName);
                this.dataGridViewCustomerName.Dock = DockStyle.Fill;

                customTabControlCustomerChannel.TabPages.Add("AddressArea", "Territory    ", 5);
                customTabControlCustomerChannel.TabPages[2].Controls.Add(this.dataGridViewAddressArea);
                this.dataGridViewAddressArea.Dock = DockStyle.Fill;



                customTabControlCustomerChannel.DisplayStyle = TabStyle.VisualStudio;
                customTabControlCustomerChannel.DisplayStyleProvider.ImageAlign = ContentAlignment.MiddleLeft;
                this.splitContainerItemCustomer.Panel2.Controls.Add(customTabControlCustomerChannel);
                customTabControlCustomerChannel.Dock = DockStyle.Fill;


                customTabControlMechanicScheme.TabPages.Add("MechanicScheme", "Mechanics ", 2);
                customTabControlMechanicScheme.TabPages[0].Controls.Add(this.dataGridViewMechanicScheme);
                this.dataGridViewMechanicScheme.Dock = DockStyle.Fill;


                customTabControlMechanicScheme.DisplayStyle = TabStyle.VisualStudio;
                customTabControlMechanicScheme.DisplayStyleProvider.ImageAlign = ContentAlignment.MiddleLeft;
                this.tableLayoutPanelMaster.Controls.Add(customTabControlMechanicScheme, 0, 7);
                this.tableLayoutPanelMaster.SetColumnSpan(customTabControlMechanicScheme, 5);
                customTabControlMechanicScheme.Dock = DockStyle.Fill;
                customTabControlMechanicScheme.Margin = this.checkBoxIsDirty.Margin;

                customTabControlItemCategory.TabPages.Add("ItemCategory", "Category   ", 4);
                customTabControlItemCategory.TabPages[0].Controls.Add(this.dataGridViewItemCategory);
                this.dataGridViewItemCategory.Dock = DockStyle.Fill;

                customTabControlItemCategory.TabPages.Add("ItemCommodity", "Commodity", 9);
                customTabControlItemCategory.TabPages[1].Controls.Add(this.dataGridViewItemCommodity);
                this.dataGridViewItemCommodity.Dock = DockStyle.Fill;

                customTabControlItemCategory.DisplayStyle = TabStyle.VisualStudio;
                customTabControlItemCategory.DisplayStyleProvider.ImageAlign = ContentAlignment.MiddleLeft;
                this.splitContainerItemCustomer.Panel1.Controls.Add(customTabControlItemCategory);
                customTabControlItemCategory.Dock = DockStyle.Fill;
            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }
        }

        private void MarketingProgram_Load(object sender, EventArgs e)
        {
            try
            {
                InitializeCommonControlBinding();

                InitializeDataGridBinding();

                InitializeReadOnlyModeBinding();
            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }
        }


        #region <InitializeBinding>

        Binding salesmenIDBinding;

        Binding marketingProgramGroupIDBinding;
        Binding marketingProgramTypeIDBinding;
        Binding customerChannelIDBinding;
        Binding timeBaseIDBinding;

        Binding referenceBinding;
        Binding referenceCodeBinding;
        Binding marketingProgramNameBinding;
        Binding marketingProgramSubNameBinding;
        Binding remarksBinding;

        Binding beginingDateBinding;
        Binding endingDateBinding;

        Binding salesTargetBinding;
        Binding marketingBudgetBinding;

        Binding inActiveBinding;
        Binding isDirtyBinding;

        private void InitializeCommonControlBinding()
        {
            try
            {
                CommonMetaList commonMetaList = new CommonMetaList();


                ERmgrUIP.ListStaffNameDataTable listStaffNameDataTable = commonMetaList.GetStaffName();
                this.comboBoxSalesmenID.DataSource = listStaffNameDataTable;
                this.comboBoxSalesmenID.DisplayMember = listStaffNameDataTable.DescriptionOfficialColumn.ColumnName;
                this.comboBoxSalesmenID.ValueMember = listStaffNameDataTable.StaffIDColumn.ColumnName;
                this.salesmenIDBinding = this.comboBoxSalesmenID.DataBindings.Add("SelectedValue", this.marketingProgramBLL.MarketingProgramMaster, "SalesmenID", true);

                ERmgrUIP.ListMarketingProgramGroupDataTable listMarketingProgramGroupDataTable = commonMetaList.GetMarketingProgramGroup();
                this.comboBoxMarketingProgramGroupID.DataSource = listMarketingProgramGroupDataTable;
                this.comboBoxMarketingProgramGroupID.DisplayMember = listMarketingProgramGroupDataTable.DescriptionColumn.ColumnName;
                this.comboBoxMarketingProgramGroupID.ValueMember = listMarketingProgramGroupDataTable.MarketingProgramGroupIDColumn.ColumnName;
                this.marketingProgramGroupIDBinding = this.comboBoxMarketingProgramGroupID.DataBindings.Add("SelectedValue", this.marketingProgramBLL.MarketingProgramMaster, "MarketingProgramGroupID", true);

                ERmgrUIP.ListMarketingProgramTypeDataTable listMarketingProgramTypeDataTable = commonMetaList.GetMarketingProgramType();
                this.comboBoxMarketingProgramTypeID.DataSource = listMarketingProgramTypeDataTable;
                this.comboBoxMarketingProgramTypeID.DisplayMember = listMarketingProgramTypeDataTable.DescriptionColumn.ColumnName;
                this.comboBoxMarketingProgramTypeID.ValueMember = listMarketingProgramTypeDataTable.MarketingProgramTypeIDColumn.ColumnName;
                this.marketingProgramTypeIDBinding = this.comboBoxMarketingProgramTypeID.DataBindings.Add("SelectedValue", this.marketingProgramBLL.MarketingProgramMaster, "MarketingProgramTypeID", true);

                ERmgrUIP.ListCustomerChannelDataTable listCustomerChannelDataTable = commonMetaList.GetCustomerChannel(false, true);
                this.comboBoxCustomerChannelID.DataSource = listCustomerChannelDataTable;
                this.comboBoxCustomerChannelID.DisplayMember = listCustomerChannelDataTable.DescriptionColumn.ColumnName;
                this.comboBoxCustomerChannelID.ValueMember = listCustomerChannelDataTable.CustomerChannelIDColumn.ColumnName;
                this.customerChannelIDBinding = this.comboBoxCustomerChannelID.DataBindings.Add("SelectedValue", this.marketingProgramBLL.MarketingProgramMaster, "CustomerChannelID", true);

                ERmgrUIP.ListTimeBaseDataTable listTimeBaseDataTable = commonMetaList.GetTimeBase();
                this.comboBoxTimeBaseID.DataSource = listTimeBaseDataTable;
                this.comboBoxTimeBaseID.DisplayMember = listTimeBaseDataTable.DescriptionColumn.ColumnName;
                this.comboBoxTimeBaseID.ValueMember = listTimeBaseDataTable.TimeBaseIDColumn.ColumnName;
                this.timeBaseIDBinding = this.comboBoxTimeBaseID.DataBindings.Add("SelectedValue", this.marketingProgramBLL.MarketingProgramMaster, "TimeBaseID", true);



                this.referenceBinding = this.textBoxReference.DataBindings.Add("Text", this.marketingProgramBLL.MarketingProgramMaster, "Reference", true);
                this.referenceCodeBinding = this.textBoxReferenceCode.DataBindings.Add("Text", this.marketingProgramBLL.MarketingProgramMaster, "ReferenceCode", true);
                this.marketingProgramNameBinding = this.textBoxMarketingProgramName.DataBindings.Add("Text", this.marketingProgramBLL.MarketingProgramMaster, "MarketingProgramName", true);
                this.marketingProgramSubNameBinding = this.textBoxMarketingProgramSubName.DataBindings.Add("Text", this.marketingProgramBLL.MarketingProgramMaster, "MarketingProgramSubName", true);
                this.remarksBinding = this.textBoxRemarks.DataBindings.Add("Text", this.marketingProgramBLL.MarketingProgramMaster, "Remarks", true);


                this.beginingDateBinding = this.dateTimePickerBiginingDate.DataBindings.Add("Value", this.marketingProgramBLL.MarketingProgramMaster, "BeginingDate", true);
                this.endingDateBinding = this.dateTimePickerEndingDate.DataBindings.Add("Value", this.marketingProgramBLL.MarketingProgramMaster, "EndingDate", true);

                this.salesTargetBinding = this.textBoxSalesTarget.DataBindings.Add("Text", this.marketingProgramBLL.MarketingProgramMaster, "SalesTarget", true, DataSourceUpdateMode.OnValidation, 0, GlobalVariables.zeroDecimalFormat);
                this.marketingBudgetBinding = this.textBoxMarketingBudget.DataBindings.Add("Text", this.marketingProgramBLL.MarketingProgramMaster, "MarketingBudget", true, DataSourceUpdateMode.OnValidation, 0, GlobalVariables.zeroDecimalFormat);

                this.inActiveBinding = this.checkBoxInActive.DataBindings.Add("Checked", this.marketingProgramBLL.MarketingProgramMaster, "InActive", true);
                this.isDirtyBinding = this.checkBoxIsDirty.DataBindings.Add("Checked", this.marketingProgramBLL.MarketingProgramMaster, "IsDirty", true);



                this.salesmenIDBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
                this.marketingProgramGroupIDBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
                this.marketingProgramTypeIDBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
                this.customerChannelIDBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
                this.timeBaseIDBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);

                this.referenceBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
                this.referenceCodeBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
                this.marketingProgramNameBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
                this.marketingProgramSubNameBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
                this.remarksBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);

                this.beginingDateBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
                this.endingDateBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);

                this.salesTargetBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
                this.marketingBudgetBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);

                this.inActiveBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);
                this.isDirtyBinding.BindingComplete += new BindingCompleteEventHandler(CommonControl_BindingComplete);


                this.naviGroupDetails.DataBindings.Add("ExpandedHeight", this.numericUpDownSizingDetail, "Value", true, DataSourceUpdateMode.OnPropertyChanged);
                this.numericUpDownSizingDetail.Minimum = this.naviGroupDetails.HeaderHeight * 2;
                this.numericUpDownSizingDetail.Maximum = this.naviGroupDetails.Height + this.dataListViewMaster.Height;

                this.tableLayoutPanelMaster.ColumnStyles[this.tableLayoutPanelMaster.ColumnCount - 1].SizeType = SizeType.Absolute; this.tableLayoutPanelMaster.ColumnStyles[this.tableLayoutPanelMaster.ColumnCount - 1].Width = 11;

                this.errorProviderMaster.DataSource = this.marketingProgramBLL.MarketingProgramMaster;

            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }
        }

        private void naviGroupDetails_HeaderMouseClick(object sender, MouseEventArgs e)
        {
            this.numericUpDownSizingDetail.Visible = this.naviGroupDetails.Expanded;
        }

        private void numericUpDownSizingDetail_ValueChanged(object sender, EventArgs e)
        {
            this.naviGroupDetails.Expand();
        }

        private void CommonControl_BindingComplete(object sender, BindingCompleteEventArgs e)
        {
            if (e.BindingCompleteState == BindingCompleteState.Exception) { GlobalExceptionHandler.ShowExceptionMessageBox(this, e.ErrorText); e.Cancel = true; }
        }

        private void GetMasterList()
        {
            foreach (OLVColumn olvColumn in dataListViewMaster.Columns) { olvColumn.Renderer = null; }

            DataTable marketingProgramMasterList = this.marketingProgramBLL.MarketingProgramMasterList(GlobalVariables.GlobalOptionSetting.LowerFillterDate, GlobalVariables.GlobalOptionSetting.UpperFillterDate);
            marketingProgramMasterList.TableName = "MarketingProgramListing";

            DataSet bindingDataSet = new DataSet();
            bindingDataSet.Tables.Add(marketingProgramMasterList);

            this.dataListViewMaster.AutoGenerateColumns = false;
            this.dataListViewMaster.DataSource = marketingProgramMasterList;
        }

        private void InitializeDataGridBinding()
        {
            try
            {

                this.GetMasterList();

                #region <dataGridViewDetail>
                CommonMetaList commonMetaList = new CommonMetaList();
                DataGridViewComboBoxColumn comboBoxColumn;


                //<dataGridViewItemCategory>
                ERmgrUIP.ListItemCategoryDataTable listItemCategoryDataTable = commonMetaList.GetItemCategory(true);
                comboBoxColumn = (DataGridViewComboBoxColumn)this.dataGridViewItemCategory.Columns[listItemCategoryDataTable.ItemCategoryIDColumn.ColumnName];
                comboBoxColumn.DataSource = listItemCategoryDataTable;
                comboBoxColumn.DisplayMember = listItemCategoryDataTable.DescriptionColumn.ColumnName;
                comboBoxColumn.ValueMember = listItemCategoryDataTable.ItemCategoryIDColumn.ColumnName;

                this.dataGridViewItemCategory.AutoGenerateColumns = false;
                this.dataGridViewItemCategory.DataSource = this.marketingProgramBLL.ItemCategoryList;

                StackedHeaderDecorator stackedHeaderDecoratorItemCategory = new StackedHeaderDecorator(this.dataGridViewItemCategory);


                //<dataGridViewItemCommodity>
                ERmgrUIP.ListItemCommodityDataTable listItemCommodityDataTable = commonMetaList.GetItemCommodity(true);
                comboBoxColumn = (DataGridViewComboBoxColumn)this.dataGridViewItemCommodity.Columns[listItemCommodityDataTable.CommodityIDColumn.ColumnName];
                comboBoxColumn.DataSource = listItemCommodityDataTable;
                comboBoxColumn.DisplayMember = listItemCommodityDataTable.DescriptionOfficialColumn.ColumnName;
                comboBoxColumn.ValueMember = listItemCommodityDataTable.CommodityIDColumn.ColumnName;

                this.dataGridViewItemCommodity.AutoGenerateColumns = false;
                this.dataGridViewItemCommodity.DataSource = this.marketingProgramBLL.ItemCommodityList;

                StackedHeaderDecorator stackedHeaderDecoratorItemCommodity = new StackedHeaderDecorator(this.dataGridViewItemCommodity);


                //<dataGridViewAddressArea>
                ERmgrUIP.ListAddressAreaDataTable listAddressAreaDataTable = commonMetaList.GetAddressArea(true);
                comboBoxColumn = (DataGridViewComboBoxColumn)this.dataGridViewAddressArea.Columns[listAddressAreaDataTable.AddressAreaIDColumn.ColumnName];
                comboBoxColumn.DataSource = listAddressAreaDataTable;
                comboBoxColumn.DisplayMember = listAddressAreaDataTable.DescriptionColumn.ColumnName;
                comboBoxColumn.ValueMember = listAddressAreaDataTable.AddressAreaIDColumn.ColumnName;

                this.dataGridViewAddressArea.AutoGenerateColumns = false;
                this.dataGridViewAddressArea.DataSource = this.marketingProgramBLL.AddressAreaList;

                StackedHeaderDecorator stackedHeaderDecoratorAddressArea = new StackedHeaderDecorator(this.dataGridViewAddressArea);


                //<dataGridViewCustomerChannel>
                ERmgrUIP.ListCustomerChannelDataTable listCustomerChannelDataTable = commonMetaList.GetCustomerChannel(true);
                comboBoxColumn = (DataGridViewComboBoxColumn)this.dataGridViewCustomerChannel.Columns[listCustomerChannelDataTable.CustomerChannelIDColumn.ColumnName];
                comboBoxColumn.DataSource = listCustomerChannelDataTable;
                comboBoxColumn.DisplayMember = listCustomerChannelDataTable.DescriptionColumn.ColumnName;
                comboBoxColumn.ValueMember = listCustomerChannelDataTable.CustomerChannelIDColumn.ColumnName;

                this.dataGridViewCustomerChannel.AutoGenerateColumns = false;
                this.dataGridViewCustomerChannel.DataSource = this.marketingProgramBLL.CustomerChannelList;

                StackedHeaderDecorator stackedHeaderDecoratorCustomerChannel = new StackedHeaderDecorator(this.dataGridViewCustomerChannel);


                //<dataGridViewCustomerName>
                ERmgrUIP.ListCustomerNameDataTable listCustomerNameDataTable = commonMetaList.GetCustomerName(true);
                comboBoxColumn = (DataGridViewComboBoxColumn)this.dataGridViewCustomerName.Columns[listCustomerNameDataTable.CustomerIDColumn.ColumnName];
                comboBoxColumn.DataSource = listCustomerNameDataTable;
                comboBoxColumn.DisplayMember = listCustomerNameDataTable.DescriptionColumn.ColumnName;
                comboBoxColumn.ValueMember = listCustomerNameDataTable.CustomerIDColumn.ColumnName;

                //--Display the second column for customer (Readonly): DescriptionOfficial -- Later: Try other way, instead of current DataGridViewComboBoxColumn.Datasource  
                comboBoxColumn = (DataGridViewComboBoxColumn)this.dataGridViewCustomerName.Columns[listCustomerNameDataTable.DescriptionOfficialColumn.ColumnName];
                comboBoxColumn.DataSource = listCustomerNameDataTable;
                comboBoxColumn.DisplayMember = listCustomerNameDataTable.DescriptionOfficialColumn.ColumnName;
                comboBoxColumn.ValueMember = listCustomerNameDataTable.CustomerIDColumn.ColumnName;

                this.dataGridViewCustomerName.AutoGenerateColumns = false;
                this.dataGridViewCustomerName.DataSource = this.marketingProgramBLL.CustomerNameList;

                StackedHeaderDecorator stackedHeaderDecoratorCustomerName = new StackedHeaderDecorator(this.dataGridViewCustomerName);


                //<dataGridViewMechanicScheme>
                ERmgrUIP.ListMechanicSchemeDataTable listMechanicSchemeDataTable = commonMetaList.GetMechanicScheme(true);
                comboBoxColumn = (DataGridViewComboBoxColumn)this.dataGridViewMechanicScheme.Columns[listMechanicSchemeDataTable.MechanicSchemeIDColumn.ColumnName];
                comboBoxColumn.DataSource = listMechanicSchemeDataTable;
                comboBoxColumn.DisplayMember = listMechanicSchemeDataTable.DescriptionColumn.ColumnName;
                comboBoxColumn.ValueMember = listMechanicSchemeDataTable.MechanicSchemeIDColumn.ColumnName;

                this.dataGridViewMechanicScheme.AutoGenerateColumns = false;
                this.dataGridViewMechanicScheme.DataSource = this.marketingProgramBLL.MechanicSchemeList;

                StackedHeaderDecorator stackedHeaderDecoratorMechanicScheme = new StackedHeaderDecorator(this.dataGridViewMechanicScheme);
                #endregion <dataGridViewDetail>
            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }

        }

        private void InitializeReadOnlyModeBinding()
        {
            try
            {
                List<Control> controlList = GlobalStaticFunction.GetAllControls(this);

                foreach (Control control in controlList)
                {
                    //if (control is TextBox) control.DataBindings.Add("Readonly", this, "ReadonlyMode");
                    if (control is TextBox) control.DataBindings.Add("Enabled", this, "EditableMode");
                    else if (control is ComboBox || control is DateTimePicker) control.DataBindings.Add("Enabled", this, "EditableMode");
                    else if (control is DataGridView)
                    {
                        control.DataBindings.Add("Readonly", this, "ReadonlyMode");
                        control.DataBindings.Add("AllowUserToAddRows", this, "EditableMode");
                        control.DataBindings.Add("AllowUserToDeleteRows", this, "EditableMode");
                    }
                }

                this.dataListViewMaster.DataBindings.Add("Enabled", this, "ReadonlyMode");
            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }
        }

        #endregion <InitializeBinding>

        private void dataListViewMaster_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                ObjectListView objectListView = (ObjectListView)sender;
                DataRowView row = (DataRowView)objectListView.SelectedObject;

                if (row != null)
                {
                    int marketingProgramID;

                    if (int.TryParse(row.Row["MarketingProgramID"].ToString(), out marketingProgramID))
                        if (marketingProgramID != this.marketingProgramBLL.MarketingProgramID) this.marketingProgramBLL.Fill(marketingProgramID);
                        else this.marketingProgramBLL.Fill(0);
                }
                else this.marketingProgramBLL.Fill(0);
            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }
        }


        private void dataGridViewMechanicScheme_CellToolTipTextNeeded(object sender, DataGridViewCellToolTipTextNeededEventArgs e)
        {
            try
            {
                if (e.RowIndex > -1 && e.RowIndex < this.dataGridViewMechanicScheme.Rows.Count && e.ColumnIndex > -1 && e.ColumnIndex < this.dataGridViewMechanicScheme.ColumnCount)
                    e.ToolTipText = this.dataGridViewMechanicScheme.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
            }
            catch (Exception exception)
            { }//                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
        }






        #region Import Excel
        private void toolStripMenuCustomerImport_Click(object sender, EventArgs e)
        {
            this.ImportExcel(OleDbDatabase.MappingTaskID.MarketingCustomer);
        }


        private void ImportExcel(OleDbDatabase.MappingTaskID mappingTaskID)
        {
            try
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.Filter = "Excel File (.xlsx)|*.xlsx";

                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    DialogMapExcelColumn dialogMapExcelColumn = new DialogMapExcelColumn(mappingTaskID, openFileDialog.FileName);

                    if (dialogMapExcelColumn.ShowDialog() == DialogResult.OK)
                    {
                        dialogMapExcelColumn.Dispose(); bool importSuccesfull = false;
                        if (mappingTaskID == OleDbDatabase.MappingTaskID.MarketingProgram)
                            importSuccesfull = this.marketingProgramBLL.ImportMarketingProgram(openFileDialog.FileName);
                        else
                            if (mappingTaskID == OleDbDatabase.MappingTaskID.MarketingCustomer)
                                importSuccesfull = this.marketingProgramBLL.ImportMarketingCustomer(openFileDialog.FileName);

                        if (importSuccesfull) MessageBox.Show(this, "Congratulation!" + "\r\n" + "\r\n" + "File: " + openFileDialog.FileName + " is imported successfull!" + "\r\n" + "\r\n" + "Please press OK to finish.", "Importing", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }

            }
            catch (Exception exception)
            {
                GlobalExceptionHandler.ShowExceptionMessageBox(this, exception);
            }

        }

        #endregion Import Excel






















    }




    #region Comparers - used to sort CustomerInfo objects and DataRows of a DataTable
    /// <summary>
    /// reusable custom DataRow comparer implementation, can be used to sort DataTables
    /// </summary>
    public class DataRowComparer : IComparer
    {
        ListSortDirection direction;
        int columnIndex;

        public DataRowComparer(int columnIndex, ListSortDirection direction)
        {
            this.columnIndex = columnIndex;
            this.direction = direction;
        }

        #region IComparer Members

        public int Compare(object x, object y)
        {

            DataRow obj1 = (DataRow)x;
            DataRow obj2 = (DataRow)y;
            return string.Compare(obj1[columnIndex].ToString(), obj2[columnIndex].ToString()) * (direction == ListSortDirection.Ascending ? 1 : -1);
        }
        #endregion
    }
    #endregion Comparers


}
